package com.wswy.wzcx.widget.home;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.blankj.utilcode.util.SizeUtils;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.CarColors;

public class HomeIndicatorView extends View{

  public HomeIndicatorView(Context context) {
    this(context,null);
  }

  public HomeIndicatorView(Context context,
      @Nullable AttributeSet attrs) {
    this(context, attrs,0);
  }

  public HomeIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);


    defaultColor = Color.parseColor("#33ffffff");
    indicatorWidth = SizeUtils.dp2px(4);
    indicatorHeight = SizeUtils.dp2px(4);
    spaceWidth = SizeUtils.dp2px(4);
    radius = indicatorWidth/2f;
    paint.setStyle(Style.FILL);

  }

  private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

  private int defaultColor;
  private int selectedColor;

  private int[] selectedColors;

  private Rect[] rects;

  private int indicatorWidth,indicatorHeight,spaceWidth;

  private int selectedPosition = 0;

  private float radius;

  public void setDefaultColor(int defaultColor) {
    this.defaultColor = defaultColor;
  }

  public void setSelectedColor(int selectedColor) {
    this.selectedColor = selectedColor;
  }

  public void setSelectedColors(int[] selectedColors) {
    this.selectedColors = selectedColors;
    preRects();
    if (selectedColors.length > 1){
      setVisibility(VISIBLE);
    }else {
      setVisibility(INVISIBLE);
    }

  }

  public void useDefaultColors(int size){

    int[] colors = new int[size];

    for (int i = 0; i < size; i++) {

      //colors[i] = ContextCompat.getColor(getContext(),CarColors.getColorStyle(i).indicatorColor);

      if (selectedColor != 0){
        colors[i] = selectedColor;
      }else {
        colors[i] = ContextCompat.getColor(getContext(),R.color.white);
      }


    }

    setSelectedColors(colors);
  }

  public void setRadius(float radius){
    this.radius = radius;
    indicatorWidth = indicatorHeight = (int) radius *2 ;
  }

  public void setSpaceWidth(int spaceWidth) {
    this.spaceWidth = spaceWidth;
  }

  private void preRects(){
    rects = new Rect[selectedColors.length];

    for (int i = 0; i < rects.length; i++) {
      Rect rect = new Rect();
      rect.top =0;
      rect.left=(indicatorWidth+spaceWidth)*i;

      rect.right = rect.left+indicatorWidth;
      rect.bottom=indicatorHeight;

      rects[i] = rect;
    }
    requestLayout();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(rects != null && rects.length > 0){
      for (int i = 0; i < rects.length; i++) {
        if(i == selectedPosition){
          paint.setColor(selectedColors[i]);
        }else {
          paint.setColor(defaultColor);
        }

        canvas.drawCircle(rects[i].centerX(),rects[i].centerY(),radius,paint);
        //canvas.drawRect(rects[i],paint);
      }
    }

  }


  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if(rects != null && rects.length > 0){
      int maxW = rects.length*(indicatorWidth)+(Math.max(rects.length-1,0))*spaceWidth;
      int h = indicatorHeight;
      setMeasuredDimension(maxW,h);
    }

  }

  public void setSelectedPosition(int selectedPosition) {
    this.selectedPosition = selectedPosition;
    invalidate();
  }


  public int getSelectedPosition() {
    return selectedPosition;
  }

}
